<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\pagniateable;
use App\Http\Resources\KeyValueResource;
use App\Models\Customer;
use Cassandra\Custom;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Lrental\Utils\OrderNumberGenerator;

class CustomerController extends Controller
{
    use pagniateable;

    public function create()
    {
        $isExists = Customer::whereName(request()->get("name"))->count() > 0;
        if ($isExists) {
            $error = true;
            $message = "名称已经存在";
            return response()->json(compact(['error', 'message']));
        }
        $customer = Customer::create(request()->all());
        return response()->json(compact(['customer']));
    }

    public function update($id)
    {
        $customer = Customer::findOrFail($id);
        $customer->update(request()->all());
        return response()->json(compact(['customer']));
    }

    public function del($id)
    {
        $customer = Customer::findOrFail($id);
        $customer->delete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }

    public function list()
    {
        $query = Customer::select("*");
        return $this->paginate($query);
    }

    public function listAll()
    {
        $all = Customer::all();
        return KeyValueResource::collection(Customer::all());
    }
}
